﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.UI.common;
using rock.common;

namespace rock.UI.custom
{
    public partial class FormSeleteRoom : Form
    {
        DalHOUSE_ROOM dal = new DalHOUSE_ROOM();
        public FormSeleteRoom()
        {
            InitializeComponent();
            //this.AcceptButton = this.button1;
            //this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;

            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;

            this.userControlCommonListHeader1.searchClick = searchClick;

            this.userControlCommonListPage1.pageClick = refreshData;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }
        private int CURRENT_PAGE = 1;

        private void Form1SeleteCustom_Load(object sender, EventArgs e)
        {
            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;
            this.userControlCommonListPage1.pageClick = refreshData;
            this.userControlCommonListHeader1.searchClick = searchClick;
             refreshData(1);
        }
        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;
            }

            this.refreshData(this.CURRENT_PAGE);
        }

        int XiaoQuSeqid = 0;
        int DongSeqid = 0;
        //刷新数据
        public void refreshData(int goPage)
        {


            CURRENT_PAGE = goPage;
            String search = this.userControlCommonListHeader1.textBox1.Text;

            StringBuilder where = new StringBuilder(
                 "(" +
                         " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
                " OR " + " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
                " OR " + " HOUSE_OWNER.NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
                ")"
                );
            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }

            where.Append(" AND HOUSE_ROOM.STATUS =  " + Constant.HOUSE_ROOM_STATUS_UNRENT);

            PageDataSet ds = dal.GetListByPage(where.ToString(), " XIAOQU_NAME  ASC ,DONG_NAME ASC ,ROOM_FLOOR ASC ", CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView1.DataSource = ds.DataSet.Tables[0];
            this.userControlCommonListPage1.Inits(ds.AllRecord, this.CURRENT_PAGE);


        }

        private void searchClick()
        {
            refreshData(CURRENT_PAGE);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

            selectedOk(Convert.ToInt32(seqid) , 2);
            this.Close();

        }
        // 1:CUSTOM 2:ROOM
        public  delegate void SelectedOk(int seqid , int type);
        public SelectedOk selectedOk;

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            setStatusWithDataGrid(dataGridView1);
        }
        public void setStatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR"];
                    dgvcc.Value = Constant.getByRoomStatus(status).Name;
                }
                catch (Exception)
                {

                }
            }
        }

    }
}
